clear;close all;clc;

% we read in our single-source recordings to create our own models at 4
% frequencies.

freqs = [200 300 500 600 800 1000 1200 1500 2000 3000];
nturns=[0.1 0.4 0.8 0.7 0.8 1.2 1.3 1.4 1.8 2.2];
% rad=0.2;
rad=0.1;
Norder=2;
thre = 0.0;


% tdur=0.02; % formal
% tdur=0.04; % formal
% tdur=0.08; % formal
% tdur=1; % formal

% freqs= [291 600 1500 3000]; %just a random frequency
% freqs= [300 600 850 900 1000 2000]; %just a random frequency
% freqs= [ 850 900 1000]; %just a random frequency
% nturns = [0.98 1.09 1.13];
% freqs= [  910]; %just a random frequency
% nturns = [ 1.13];
fs = 44100;
% whos
plot_each =1;

projection1 = 0; % neariest center
Num_centers = 1;
% Proto_Num = 1.2;
Proto_Num = 21;

Angles = -90:10:90;
sides = {'Left','Right'};


f2=figure('position',[100 70 800 500]);

for ifreql = 1:length(freqs)
    % for ifreql = 1
    f1=figure('position',[100 70 1600 700]);
    freq = freqs(ifreql)
    tdur = 40/freq;
    for iangle = 1:length(Angles)
        figure(f1)
        subplot(3,7,iangle);
        % cc = 0;
        Angle = Angles(iangle);
        PString = num2str(Proto_Num);
        if Angle < 0
            % eval(['load All_sound_recordings\Protocol_' PString(1) '\Protocol_' PString '_' sides{1} '_'   num2str(abs(Angles(iangle))) 'degree.mat']);
            eval(['load All_sound_recordings\Protocol_' PString '\Phoneme_' PString '_' sides{1} '_'   num2str(abs(Angles(iangle))) 'degree.mat']);
        elseif Angle ==0
            % eval(['load All_sound_recordings\Protocol_' PString(1) '\Protocol_' PString '_' sides{1} '_'   num2str(abs(Angles(iangle))) 'degree.mat']);
            eval(['load All_sound_recordings\Protocol_' PString '\Phoneme_' PString '_'  num2str(Angles(iangle)) 'degree.mat']);
        else
            % eval(['load All_sound_recordings\Protocol_' PString(1) '\Protocol_' PString '_' sides{1} '_'   num2str(abs(Angles(iangle))) 'degree.mat']);
            eval(['load All_sound_recordings\Protocol_' PString '\Phoneme_' PString '_' sides{2} '_'   num2str(Angles(iangle)) 'degree.mat']);
        end

        beginP = (ifreql-1).*20.*fs+1;
        endP = ifreql.*20.*fs;

% figure
% NL = length(recorded_sound(:,1));
% tsig = (1:NL)./fs;
% plot(tsig,recorded_sound(:,1))
% pause
% stop0

        recorded_sound=recorded_sound(4.*fs+beginP:4.*fs+endP,:);
        left_sound = recorded_sound(:,1);
        right_sound = recorded_sound(:,2);
        [SL,SR,freq_pos,flag]=mySTFT(left_sound,right_sound,fs,thre,tdur);
        % normalization:
        % [SL1,SR1]=Normalize(SL,SR);
        % if Angle < 0
        %     L_or_R=1;
        % else
        L_or_R=2;
        % end
        SLR=Normalize_Both(SL,SR,L_or_R);
        [Y Ind] = min(abs(freq_pos - freq));
        X_P = [real(SLR(Ind,flag>0))' imag(SLR(Ind,flag>0))'];
        plot(X_P(:,1),X_P(:,2),'k.');hold on;axis square
        % end

        out = f_mycenter(X_P,rad);
        F_centers(iangle,:) = out.C1;

        s=plot(F_centers(iangle,1),F_centers(iangle,2),'go');hold on;set(s,'linewidth',2)
        %         grid on
        set(gca,'Xtick',-1:0.5:1)
        set(gca,'Ytick',-1:0.5:1)
        if Angle < 0
            Pangle =['-' num2str(abs(Angles(iangle))) ];
        elseif Angle ==0
            Pangle =[num2str(abs(Angles(iangle))) ];
        else
            Pangle =['+' num2str(abs(Angles(iangle))) ];
        end
        title([num2str(freq) 'Hz;' Pangle]);
        axis([-1 1 -1 1])

        figure(f2)
        subplot(2,5,ifreql)
        s=text(F_centers(iangle,1),F_centers(iangle,2),Pangle);hold on;set(s,'fontsize',7)
        hold on;
        axis([-1 1 -1 1])
        pause(0.01)
    end % angle

    hold on;
    plot(-1:1,[0,0,0],'k', 'LineWidth', 0.5);
    plot([0,0,0],-1:1,'k','LineWidth',0.5)
    set(gca,'fontsize',10)
    xlabel('Re','fontsize',12)
    ylabel('Im','fontsize',12)
    hold on

    figure(f2)
    subplot(2,5,ifreql)
    plot(F_centers(:,1),F_centers(:,2),'r')
    title([num2str(freq) ' Hz'])
    axis([-1 1 -1 1]);axis square
    pause(0.1)
    %==============================
    out=f_get_spiral(F_centers,Norder,nturns(ifreql));
    x_spiral=out.x_spiral;
    y_spiral=out.y_spiral;
    plot(x_spiral,y_spiral,'color',[0.5 0.5 0.5]) ; % nturns crossings, including end point
    axis square

    eval(['save Freq_centers_own' num2str(freq) ' F_centers  x_spiral y_spiral'])
    clear F_centers
end % freq




% figure;
% pc = 'bkrm';
% for ifreq = 1:4
%     s=plot([fliplr(-Angles) Angles],[fliplr(record_class{1}(ifreq,:)) record_class{2}(ifreq,:)]',pc(ifreq));hold on;
%     set(s,'linewidth',2)
%     if ifreq<3
%         set(s,'linestyle',':')
%     end
% end
% plot([-100 100],[-100 100],'g');
% legend('300 Hz',' 600 Hz',' 1500 Hz',' 3000 Hz','Perfect','Location','northwest')
% legend boxoff
% xlabel('True Angle (degrees)','fontsize',12)
% ylabel('classified Angle (degrees)','fontsize',12)
% title('Single Source')
